﻿using DevExpress.Utils;
using Lamex.Base;
using Lamex.Common;
using Lamex.Interface;
using Lamex.Library;
using Lamex.Library.CommonClass;
using Lamex.SIESystemManager.BLL;
using Lamex.SIESystemManager.en_us;
using Lamex.SIESystemManager.Model;
using Lamex.SIESystemManager.zh_Hans;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.SIESystemManager.UI
{
    public partial class FrmBIEAllowanceFactor : frmBaseDataForm
    {
        private string[] strList = new string[6];
        ResourceManager rm = null;
        public FrmBIEAllowanceFactor()
        {
            InitializeComponent();
            InitializeForm();
            this.Load += new EventHandler(FrmBIEAllowanceFactor_Load);
            this.TextChanged += new EventHandler(FrmBIEAllowanceFactor_TextChanged);
            ChangeLanguage();

            txtNUMBER.CaptionVisible = true;
            txtDESCRIPTION_NUMBER.CaptionVisible = true;
            txtALLOWANCE_FACTORS.CaptionVisible = true;

            txtNUMBER.CaptionVisible = true;
            txtDESCRIPTION_NUMBER.CaptionVisible = true;
            txtALLOWANCE_FACTORS.CaptionVisible = true;


        }
        //public string IsEdit = "";
        //public string KeysName = "";
        private void FrmBIEAllowanceFactor_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }
        private void FrmBIEAllowanceFactor_TextChanged(object sender,EventArgs e)
        {
            ChangeLanguage();
        }
        private void ChangeLanguage()
        {
            if (Loginer.CurrentUser.ChangeLanguage == 0)
                rm = new ResourceManager( ResourceIEZh.ResourceManager.BaseName,Assembly.GetExecutingAssembly());
            else
                rm = new ResourceManager(ResourceIEEng.ResourceManager.BaseName,Assembly.GetExecutingAssembly());

            this.Text = rm.GetString("FrmBIEAllowanceFactor");
            txtNUMBER.Caption = rm.GetString("txtNUMBER");
            txtALLOWANCE_FACTORS.Caption = rm.GetString("txtALLOWANCE_FACTORS");
            txtDESCRIPTION_NUMBER.Caption = rm.GetString("txtDESCRIPTION_NUMBER");
            txtEditALLOWANCE_FACTORS.Caption = rm.GetString("txtEditALLOWANCE_FACTORS");
            txtEditDESCRIPTION_NUMBER.Caption = rm.GetString("txtEditDESCRIPTION_NUMBER");
            txtEditNUMBER.Caption = rm.GetString("txtEditNUMBER");
            xtraTabPage1.Text= rm.GetString("xtraTabPage1");
            xtraTabPage2.Text= rm.GetString("xtraTabPage2");
            LanguageObject.RegisterLanguage(gridView2);
        }
        protected override void InitializeForm()
        {
            _SummaryView = new DevGridView(gridView2);
            gridView2.DoubleClick += new EventHandler(OnGridViewDoubleClick); //主表DoubleClict
            frmGridViewCust.RegisterGrid(gridView2);
            DevStyle.SetGridControlLayout(gcView, false);//表格设置   

            DevStyle.SetSummaryGridViewLayout(gridView2);
            DevStyle.SetDetailGridViewLayout(gridView2);
            ShowSummary(); //下载显示数据.
            tpSummary = xtraTabPage1;
            tcBusiness = xtraTabControl1;
            tpSummary.PageVisible = true;
            //tpDetail.PageVisible = false;
            tcBusiness.ShowTabHeader = DefaultBoolean.True;
            tcBusiness.SelectedTabPage = tpSummary;
            //ShowSummaryPage(true); //一切初始化完毕後显示SummaryPage        
        }
        
        private void ShowSummary()
        {
            try
            {
                //
                //ucLookUpEdit1.ValueMember = "UserName";
                //ucLookUpEdit1.DisPlayMember = "UserName";
                //ucLookUpEdit1.DataSource = dt;
                ////ucLookUpEdit1.UcPopulateColumns();

                ////ucLookUpEdit1.Item[1].Caption = "用户名称";
                //////ucLookUpEdit1.ForceInitialize();
                ////ucLookUpEdit1.UcItemIndex = 0;
                //DoBindingSummaryGrid(dt); //绑定主表的Grid
                //ShowSummaryPage(true); //显示Summary页面. 
                xtraTabPage2.PageEnabled = false;
                xtraTabPage1.PageEnabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        public override void DoView(Lamex.Interface.IButtonInfo sender)
        {
            string sqlWhere = "";
            string sql = " select * from B_IE_Allowance_Factor where 1=1 ";
            if (!string.IsNullOrEmpty(txtNUMBER.Text))
            {
                sqlWhere += " and NUMBER like '%" + txtNUMBER.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtDESCRIPTION_NUMBER.Text))
            {
                sqlWhere += " and DESCRIPTION_NUMBER like '%" + txtDESCRIPTION_NUMBER.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtALLOWANCE_FACTORS.Text))
            {
                sqlWhere += " and ALLOWANCE_FACTORS like '%" + txtALLOWANCE_FACTORS.Text + "%'";
            }

            sql = sql + sqlWhere;
            DataTable dt = BaseFactory<B_IE_Allowance_Factor>.Instance.GetDataTableBySql(sql);
            DoBindingSummaryGrid(dt);
            base.DoView(sender);

        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        public override void DoAdd(Lamex.Interface.IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Allowance_Factor", "LamexMESManager")) return;
            #endregion
            xtraTabPage1.PageEnabled = false;
            xtraTabPage2.PageEnabled = true;
            xtraTabPage2.PageVisible = true;
            IsEdit = "Add";
            xtraTabPage2.Tag = rm.GetString("xtraTabPage1"); ;
            base.DoAdd(sender);

        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        public override void DoEdit(Lamex.Interface.IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Allowance_Factor", "LamexMESManager")) return;
            #endregion
            if (gridView2.SelectedRowsCount == 0)
            {
                if (Loginer.CurrentUser.ChangeLanguage == 0)
                    DevExpress.XtraEditors.XtraMessageBox.Show("【没有选中要修改的数据！】", "提示", MessageBoxButtons.OK);
                else
                    DevExpress.XtraEditors.XtraMessageBox.Show("【No Choose Modify Data ！】", "提示", MessageBoxButtons.OK);
                return;
            }
            int m = gridView2.FocusedRowHandle;
            DataRow dr = gridView2.GetDataRow(m);
            KeysName = dr["IDNO"].ToString();
            //判断记录是否为最新的，若不是给出提示，避免修改出现错误
            string sql = @" select TOP 1 END_TS FROM [LamexMESManager].[dbo].[B_IE_Allowance_Factor]
                                where NUMBER='" + dr["NUMBER"].ToString() + "' AND DESCRIPTION_NUMBER='" + dr["DESCRIPTION_NUMBER"].ToString() + "' AND ALLOWANCE_FACTORS='" + dr["ALLOWANCE_FACTORS"].ToString() + "'";
            DataTable dt = BaseFactory<B_IE_Allowance_Factor>.Instance.GetDataTableBySql(sql);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0][0].ToString().Substring(0, 10) != "9999/12/31")
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        DevExpress.XtraEditors.XtraMessageBox.Show("该工序宽放系数不是最新的记录，请查找最新记录修改", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("This's Process allowanceFactor not new record,Please query new record modify", "提示", MessageBoxButtons.OK);
                    return;
                }

            }


            xtraTabPage1.PageEnabled = false;
            xtraTabPage2.PageEnabled = true;
            xtraTabPage2.PageVisible = true;

            IsEdit = "Edit";
            xtraTabPage2.Tag = rm.GetString("xtraTabPage2"); ;

            //***********************加载数据****************************//

            txtEditNUMBER.Text = dr["NUMBER"].ToString();
            txtEditDESCRIPTION_NUMBER.Text = dr["DESCRIPTION_NUMBER"].ToString();
            txtEditALLOWANCE_FACTORS.Text = dr["ALLOWANCE_FACTORS"].ToString();

            //***********************赋值给数组****************************//
            for (int i = 0; i < 6; i++)
            {
                strList[i] = dr[i + 1].ToString();
            }
            base.DoEdit(sender);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        public override void DoSave(Lamex.Interface.IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Allowance_Factor", "LamexMESManager")) return;
            #endregion
            if (txtEditNUMBER.Alter()) return;
            if (IsEdit == "Add")
            {
                //判断记录是否存在
                string sql = @" select NUMBER FROM [LamexMESManager].[dbo].[B_IE_Allowance_Factor]
                                where NUMBER='" + txtEditNUMBER.Text + "'";
                DataTable dt = BaseFactory<B_IE_Allowance_Factor>.Instance.GetDataTableBySql(sql);
                if (dt.Rows.Count > 0)
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        DevExpress.XtraEditors.XtraMessageBox.Show("该工序宽放系数已存在", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("This's AllowanceFactor has Exited", "Tips", MessageBoxButtons.OK);
                    return;
                }
                B_IE_Allowance_FactorInfo model = new B_IE_Allowance_FactorInfo();
                model.NUMBER = txtEditNUMBER.Text;//工序代码
                model.DESCRIPTION_NUMBER = txtEditDESCRIPTION_NUMBER.Text;//工序名称
                model.ALLOWANCE_FACTORS = txtEditALLOWANCE_FACTORS.Text;//宽放系数
                model.EFFECTIVE_TS = DateTime.Now;//生效时间
                model.END_TS = Convert.ToDateTime("9999-12-31 23:59:59.000");//截止时间 新增为默认值
                model.CREATE_TS = DateTime.Now;//创建时间
                model.UPDATE_USER = Lamex.Common.Loginer.CurrentUser.Account;//修改人
                if (BaseFactory<B_IE_Allowance_Factor>.Instance.Insert(model))
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        DevExpress.XtraEditors.XtraMessageBox.Show("新增成功", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("Add Successed", "Tips", MessageBoxButtons.OK);

                }
                else
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        DevExpress.XtraEditors.XtraMessageBox.Show("新增失败", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("Add Failed", "Tips", MessageBoxButtons.OK);
                }

            }
            if (IsEdit == "Edit")
            {
                B_IE_Allowance_FactorInfo model = new B_IE_Allowance_FactorInfo();
                model.IDNO = int.Parse(KeysName);
                txtEditNUMBER.Enabled = false;//修改时变为不可编辑状态
                //判断数据是否有修改，若没有变动，则提示出来
                if (strList[0] == txtEditNUMBER.Text && strList[1] == txtEditDESCRIPTION_NUMBER.Text && strList[2] == txtEditALLOWANCE_FACTORS.Text)
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        DevExpress.XtraEditors.XtraMessageBox.Show("没有修改任何一个记录", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("No Modify any Record", "Tips", MessageBoxButtons.OK);
                    return;
                }
                //更新当前记录
                //如果日期为当天则只更新不新增,否则先更新后插入新纪录
                if (DateTime.Now.ToString("yyyy/MM/dd") == Convert.ToDateTime(strList[5]).ToString("yyyy/MM/dd"))
                {
                    model.NUMBER = txtEditNUMBER.Text;//工序代码
                    model.DESCRIPTION_NUMBER = txtEditDESCRIPTION_NUMBER.Text;//工序名称
                    model.ALLOWANCE_FACTORS = txtEditALLOWANCE_FACTORS.Text;//宽放系数
                    model.EFFECTIVE_TS = Convert.ToDateTime(strList[3]);
                    model.END_TS = Convert.ToDateTime(strList[4]);
                    model.CREATE_TS = Convert.ToDateTime(strList[5]);
                    model.UPDATE_USER = Lamex.Common.Loginer.CurrentUser.Account;//修改人
                    if (BaseFactory<B_IE_Allowance_Factor>.Instance.Update(model))
                    {
                        if (Loginer.CurrentUser.ChangeLanguage == 0)
                            DevExpress.XtraEditors.XtraMessageBox.Show("修改成功", "提示", MessageBoxButtons.OK);
                        else
                            DevExpress.XtraEditors.XtraMessageBox.Show("Modify Successed", "Tips", MessageBoxButtons.OK);
                    }
                }
                else
                {
                    model.NUMBER = strList[0];
                    model.DESCRIPTION_NUMBER = strList[1];
                    model.ALLOWANCE_FACTORS = strList[2];
                    model.EFFECTIVE_TS = Convert.ToDateTime(strList[3]);
                    model.END_TS = DateTime.Now;//截止时间更改为当前时间
                    model.CREATE_TS = Convert.ToDateTime(strList[5]);
                    model.UPDATE_USER = Lamex.Common.Loginer.CurrentUser.Account;//修改人
                    if (BaseFactory<B_IE_Allowance_Factor>.Instance.Update(model))
                    {
                        //更新成功后再进行插入操作
                        model.NUMBER = txtEditNUMBER.Text;//工序代码
                        model.DESCRIPTION_NUMBER = txtEditDESCRIPTION_NUMBER.Text;//工序名称
                        model.ALLOWANCE_FACTORS = txtEditALLOWANCE_FACTORS.Text;//宽放系数
                        model.EFFECTIVE_TS = DateTime.Now.AddDays(1);//生效时间-修改时间增加1天
                        model.END_TS = Convert.ToDateTime("9999-12-31 23:59:59.000");//截止时间 新增为默认值
                        model.CREATE_TS = DateTime.Now;//创建时间
                        model.UPDATE_USER = Lamex.Common.Loginer.CurrentUser.Account;//修改人
                        if (BaseFactory<B_IE_Allowance_Factor>.Instance.Insert(model))
                        {
                            if (Loginer.CurrentUser.ChangeLanguage == 0)
                                DevExpress.XtraEditors.XtraMessageBox.Show("修改成功", "提示", MessageBoxButtons.OK);
                            else
                                DevExpress.XtraEditors.XtraMessageBox.Show("Modify Successed", "Tips", MessageBoxButtons.OK);
                        }
                        else
                        {
                            if (Loginer.CurrentUser.ChangeLanguage == 0)
                                DevExpress.XtraEditors.XtraMessageBox.Show("修改失败", "提示", MessageBoxButtons.OK);
                            else
                                DevExpress.XtraEditors.XtraMessageBox.Show("Modify Failed", "Tips", MessageBoxButtons.OK);
                        }
                    }
                    else
                    {
                        if (Loginer.CurrentUser.ChangeLanguage == 0)
                            DevExpress.XtraEditors.XtraMessageBox.Show("修改失败", "提示", MessageBoxButtons.OK);
                        else
                            DevExpress.XtraEditors.XtraMessageBox.Show("Modify Failed", "Tips", MessageBoxButtons.OK);
                    }

                }

            }

            xtraTabPage2.PageEnabled = false;
            xtraTabPage1.PageEnabled = true;
            DoView(null);
            base.DoSave(sender);

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        public override void DoDelete(Lamex.Interface.IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Allowance_Factor", "LamexMESManager")) return;
            #endregion
            if (gridView2.SelectedRowsCount == 0)
            {
                if(Loginer.CurrentUser.ChangeLanguage==0)
                    MessageBox.Show("【没有选中要删除的数据，请选择好数据！】");
                else
                    MessageBox.Show("【No Choose Delete Data,Please Choose Data】");

                return;
            }
            int m = gridView2.FocusedRowHandle;
            DataRow dr = gridView2.GetDataRow(m);
            if (dr != null)
            {
                if (BaseFactory<B_IE_Allowance_Factor>.Instance.Delete(dr["IDNO"].ToString()))
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        MessageBox.Show("【数据删除成功！】");
                    else
                        MessageBox.Show("【Data Remove Succesed！】");

                }
                else
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        MessageBox.Show("【数据删除失败！】");
                    else
                        MessageBox.Show("【Data Remove failed！】");

                }
            }
            DoView(null);
            base.DoDelete(sender);
        }
        public override void DoImport(IButtonInfo sender)
        {
            #region 按钮权限
            if (!BuildSqlHelper.IsExistList("B_IE_Allowance_Factor", "LamexMESManager")) return;
            #endregion
            FrmImport frm = new FrmImport("B_IE_Allowance_Factor");
            frm.ShowDialog();
            base.DoImport(sender);
        }

    }
}

